package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * MainMenu Class
 * 
 * Description: -
 * 
 * 
 * @package cli.menu
 * @author Lai Yong Quan (A0080909Y)
 * @editor(s) -
 */

class MainMenu extends Function {

	private static String[] MAINMENU = { "-= Main menu =-",
			"[b]: back [u]: undo [r]: redo [h]: main menu [e]: exit",
			"1. Add Entry", "2. Search", "3. Track Expenses", "4. Track Receipts",
			"5. General Report", "6. Alert Settings", "7. Manage Categories",
			"8. Manage Modes of Payment", "9. Excel Powered","", "Option: " };

	private static final State back = State.MAINMENU;
	private static final State current = State.MAINMENU;

	public static State runMenu() {

		cli.print(MAINMENU);

		String input = cli.nextLine();
	
		return processInput(input);
	}

	// helper function to handle user input
	private static State processInput(String input) {

		// full length commands not supported yet (magic word is YET)
		//if ((input == null) || (input.length() > 1))
		if(input == null || input.length() == 0){
			cli.clearScreen();
	//		cli.println("Invalid input received: " + input);
			return current;
		}
		
		boolean compactcmd = true;
		
		if(input.split(" ").length > 1){
			while(compactcmd){
				 compactcmd = cmdParser.parse(input);
				 if(!compactcmd)
					 break;
				 try {
					cli.getApi().save();
				} catch (Exception e) {
					cli.println("An error has occurred: unable to save");
				}
				 cli.print("cmd > ");
				 input = cli.nextLine();
				 if(input == null || input.length() == 0)
					 break;
			}
			return current;
		}
		
		//if it reaches here, it is not a compact command
		if(input.length() > 1){
			cli.clearScreen();
			cli.println("Invalid input received: " + input);
			return current;
		}

		State nextState = current;
		cli.clearScreen();
		char charInput = input.charAt(0);
		// see what user wants
		switch (charInput) {
		case 'e':
		case 'E':
			// CLI.api.save();
			cli.terminate();
			break; //unnecessary break actually, but for formality and to avoid confusion
		case 'u':
		case 'U':
			tools.undo();
			break;
		case 'r':
		case 'R':
			tools.redo();
			break;
		case 'h':
		case 'H':
			nextState = State.MAINMENU;
		case 'B':
		case 'b':
			nextState = back;
			break;
		case '1':
			nextState = State.ADDENTRYMENU;
			break;
		case '2':
			nextState = State.SEARCHSELECTFILTER;
			break;
		case '3':
			nextState = State.TRACKEXPENSES;
			break;
		case '4':
			nextState = State.SEARCHRECEIPTSELECTFILTER;
			break;
		case '5':
			nextState = State.GENERALREPORT;
			break;
		case '6':
			nextState = State.ALERTSETTINGS;
			break;
		case '7':
			nextState = State.MANAGECATEGORIES;
			break;
		case '8':
			nextState = State.MANAGEPAYMENTMODES;
			break;
		case '9':
			nextState = State.EXCELPOWERED;
			break;

		default: {
			cli.println("Invalid input received: " + input);
		}

		}
		return nextState;
	}
}